package freecomm.study.jumpactivity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class JumpActivity extends Activity
{
    /** Called when the activity is first created. */
    private Button directButton;
    private EditText tv1;
    private EditText tv2;
    private Button btn;
    private Button exitButton;
    private Button directActionButton;
    private Button getContentButton;
    private ListView listView;
    public static final String MY_ACTION = "freecomm.study.jumpactivity.intent.action.MY_ACTION";
    private static final int REQUEST_GET_NOTE = 1;
    private static final int REQUEST_GET_BACK = 0;
    AdapterView.OnItemClickListener listClickListener = new AdapterView.OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            // TODO Auto-generated method stub
            Intent intent = new Intent();
            String data;
            Uri uri;
            switch (position)
            {
            case 0:
                data = "content://contacts/people/1";
                uri = Uri.parse(data);
                intent.setAction(Intent.ACTION_GET_CONTENT);
                intent.setData(uri);
                startActivity(intent);
                break;
            case 1:
                data = "content://contacts/people/1";
                uri = Uri.parse(data);
                intent.setAction(Intent.ACTION_EDIT);
                intent.setData(uri);
                startActivity(intent);
                break; 
            case 2:
                data = "tel:1008611";
                uri = Uri.parse(data);
                intent.setAction(Intent.ACTION_DIAL);
                intent.setData(uri);
                startActivity(intent);
                break;
            case 3:
                data = "tel:1008611";
                uri = Uri.parse(data);
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(uri);
                startActivity(intent);
                break;

            default:
                break;
            }

        }
    };

    @Override
    protected void onPause()
    {
        super.onPause();
        Log.i("Activity Life Cycle", "On Pause");
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        Log.i("Activity Life Cycle", "On Destory");
    }

    @Override
    protected void onStop()
    {
        super.onStop();
        Log.i("Activity Life Cycle", "On Stop");
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        Log.i("Activity Life Cycle", "On Resume");
    }

    @Override
    protected void onRestart()
    {
        super.onRestart();
        Log.i("Activity Life Cycle", "On Restart");
    }

    @Override
    protected void onStart()
    {
        super.onStart();
        Log.i("Activity Life Cycle", "On Start");
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        directButton = (Button) findViewById(R.id.directButton);
        tv1 = (EditText) findViewById(R.id.editText3);
        tv2 = (EditText) findViewById(R.id.editText4);
        btn = (Button) findViewById(R.id.nextButton);
        exitButton = (Button) findViewById(R.id.exitBtn);
        getContentButton = (Button) findViewById(R.id.contentBtn);
        directActionButton = (Button) findViewById(R.id.actionJumpBtn);
        listView = (ListView) findViewById(R.id.listView1);
        String[] list =
        { "show phone info", "edit phone info", "shoe dial", "dial" };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, list);
        listView.setAdapter(adapter);
        directButton.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                Intent intent = new Intent(JumpActivity.this, DirectActivity.class);
                startActivity(intent);
            }
        });
        btn.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                Bundle b = new Bundle();
                b.putString("a", tv1.getText().toString());
                b.putString("b", tv2.getText().toString());
                Intent intent = new Intent(JumpActivity.this, NextActivity.class);
                intent.putExtras(b);
                startActivityForResult(intent, REQUEST_GET_BACK);
            }
        });
        exitButton.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                JumpActivity.this.finish();
            }
        });
        directActionButton.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setAction(MY_ACTION);
                startActivity(intent);
            }
        });
        getContentButton.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_GET_CONTENT);
                intent.setType("vnd.android.cursor.item/vnd.google.note");
                startActivity(intent);
            }
        });
        listView.setOnItemClickListener(listClickListener);
        Log.i("Activity Life Cycle", "On Create");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        switch (requestCode)
        {
        case REQUEST_GET_BACK:
            Bundle b = new Bundle();
            b = data.getExtras();
            tv1.setText(b.getString("a"));
            tv2.setText(b.getString("b"));
            break;
        case REQUEST_GET_NOTE:
            break;
        }
    }
}